import { http } from "@/utils/http";
import type { NodeInfo, NodeList } from "./types";
import type { BaseResponse, PDataRes } from "@/api/response";
import {
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";
import type { NodeMetricList } from "./metricType";

export class K8sNode {
  baseReq: MakePathReq = {
    src: "nodes",
    app: ""
  };

  getNodeList = (req: MergePathReq): BaseResponse<NodeList> => {
    const api = k8sPathMergeReqs(req, this.baseReq);
    return http.get(api);
  };

  getNodeDetail = (
    query: MergePathReq,
    data?: NodeInfo
  ): BaseResponse<NodeInfo> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.get(api, { params: query.query });
  };

  updateNode = (query: MergePathReq, data?: NodeInfo): Promise<PDataRes> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.put(api, { data });
  };

  deleteNode = (query: MergePathReq, data?: NodeInfo): Promise<PDataRes> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.delete(api, { data });
  };

  createNode = (query: MergePathReq, data?: NodeInfo): Promise<PDataRes> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.post(api, { data });
  };

  cordonNode = (
    query?: MergePathReq,
    node?: NodeInfo,
    unschedulable?: boolean
  ): BaseResponse<any> => {
    const api = k8sPathMergeReqs(query, this.baseReq, node);
    const data = {
      spec: {
        unschedulable: unschedulable
      }
    };
    return http.patch(api, { data });
  };

  getNodeMetrics = (req?: MergePathReq): BaseResponse<NodeMetricList> => {
    const api = k8sPathMergeReqs(req, {
      src: "nodes",
      app: "metrics.k8s.io/v1beta1"
    });

    return http.get(api, { params: req.query });
  };
}
